package com.zmn.plat.admin.controller.orderconfig.track;

import java.util.List;
import java.util.Objects;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.zmn.common.dto2.ResponseDTO;
import com.zmn.common.utils.string.StringUtil;
import com.zmn.plat.common.exception.PlatException;
import com.zmn.plat.model.entity.orderconfig.track.OrderTrack;

import lombok.extern.slf4j.Slf4j;

/**
 * @author tanbiao
 * @description 订单配置取消分类
 * @since 2019/10/21 16:49
 * @modifier
 **/
@SuppressWarnings("Duplicates")
@RestController
@RequestMapping("/orderconfig/track/")
@Slf4j
public class OrderTrackModifyController extends OrderTrackBaseController {

    /**
     * region 新增/修改
     *
     * @param orderTrack
     *            产品新增修改实体
     * @return ResultDTO 执行结果
     */
    @RequestMapping(value = "addEdit")
    @RequiresPermissions("/orderconfig/track/detail.action")
    public ResponseDTO addEdit(HttpServletRequest request, @RequestBody OrderTrack orderTrack) {
        String operator = getMcStaffRealName();
        boolean isNew = Objects.isNull(orderTrack.getTrackId());
        try {
            orderTrackBService.addEdit(orderTrack, operator);
            return ResponseDTO.success(true, isNew ? "新增分类成功!" : "修改分类成功");
        } catch (PlatException e) {
            return ResponseDTO.fail(e.getMessage());
        } catch (Exception ex) {
            log.error("操作失败", ex);
            return ResponseDTO.fail("操作失败！");
        }
    }

    @RequestMapping(value = "updateStatus")
    @RequiresPermissions("/orderconfig/track/updateStatus.action")
    public ResponseDTO updateStatus(HttpServletRequest request, HttpServletResponse response, @RequestParam(value = "ids", defaultValue = "") String ids,
        @RequestParam(value = "status", defaultValue = "0") int status) {

        if (StringUtil.isBlank(ids)) {
            return ResponseDTO.fail("请选择要更改状态的分类！");
        }
        List<Integer> categoryIds = StringUtil.convertStrsToList(ids);
        orderTrackService.updateStatusBatch(categoryIds, status, getMcStaffRealName());
        return ResponseDTO.success(true, "操作成功！");
    }

    /**
     * 描述:修改排序
     *
     * @param trackId
     *            分类id
     * @param sort
     *            修改的状态
     * @return: ResultDTO 执行结果
     * @auther: tanbiao
     * @since:2019/10/22
     */
    @RequestMapping(value = "updateSort")
    @RequiresPermissions("/orderconfig/track/list.action")
    public ResponseDTO updateSort(HttpServletRequest request, HttpServletResponse response, @RequestParam(value = "id", defaultValue = "0") Integer trackId,
        @RequestParam(value = "sort", defaultValue = "0") Integer sort) {

        if (trackId == 0) {
            return ResponseDTO.fail("请选择要更改排序的分类！");
        }
        if (sort == 0) {
            return ResponseDTO.fail("请设置分类排序值！");
        }
        orderTrackService.updateSortKey(trackId, sort, getMcStaffRealName());
        return ResponseDTO.success(true, "操作成功！");
    }

}
